/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.ejb.user.impl;

import com.nus.application.ejb.user.UserEjbLocal;
import com.nus.dataaccess.dao.user.UserAccount;
import com.nus.dataaccess.dao.user.UserInfo;
import com.nus.dataaccess.dao.user.dao.UserAccountDao;
import com.nus.dataaccess.dao.user.dao.UserInfoDao;
import com.nus.dataaccess.dao.util.HibernateUtil;
import java.util.List;
import java.util.Map;
import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;

/**
 *
 * @author robin.foe
 */
@Stateless
@PermitAll
public class UserEjb implements UserEjbLocal {

    @Override
    public String getTestString() {
        return "Hello this is my first EJB";
    }

    @Override
    public List<UserInfo> listAllUser() {
        return UserInfoDao.getInstance().findAll();
    }
    
    @Override
    public UserAccount doLogin(String username, String password){
        UserAccount userAccount = this.retrieveUserByUsername(username);
        if(userAccount == null)
        System.err.print("User account is empty");
        
        if(userAccount != null)
                return (password.equals(userAccount.getPassword())) ? userAccount : null;

        return userAccount;
    }

    @Override
    public UserAccount retrieveUserByUsername(String userName) {
        return UserAccountDao.getInstance().getByUserName(userName);
    }

    @Override
    public UserAccount retrieveUserById(Long userId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public UserInfo retrieveUserInfoById(Long userId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void persistUser(UserInfo userInfo) {
        try {
            HibernateUtil.beginTransaction();
            UserInfoDao.getInstance().saveOrUpdate(userInfo.getUserAccount());
            userInfo.setAccountId(userInfo.getUserAccount().getId());
            UserInfoDao.getInstance().saveOrUpdate(userInfo);
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            System.out.printf("Rolling back"+e);
            HibernateUtil.rollbackTransaction();
        }
    }

    @Override
    public List<UserInfo> paginateUserInfo(int first, int pageSize, String sortField, boolean ascending, Map<String, String> filters) {
        return UserInfoDao.getInstance().paginate(first, pageSize, sortField, ascending, filters);
    }

    @Override
    public int countPaginateUserInfo(Map<String, String> filters) {
        return UserInfoDao.getInstance().getTotalCount(filters);
    }

    @Override
    public void deleteUser(UserInfo userInfo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void changeTheme(String userName, String theme) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
